package com.ark.common.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

public class EncryptionUtils {

    private static final String ALGORITHM = "AES/GCM/NoPadding";
    private static final int KEY_SIZE = 256;
    private static final int IV_SIZE = 12;  // 96 GCM 模式的位
    private static final int TAG_LENGTH_BIT = 128;

    // 这是一个示例密钥，建议使用更安全的方式存储和加载密钥
//    @Value("${encryption.secret-key}")
    private static String SECRET_KEY="This-is-a-note--" ; // 请确保这是一个安全的密钥，并且长度为16, 24或32字节

    /**
     * AES 密钥
     */
    private static final SecretKey secretKey=new SecretKeySpec(SECRET_KEY.getBytes(), 0, SECRET_KEY.getBytes().length, "AES");

    /**
     * 加密
     * @param password 密码
     * @return 加密密码
     */
    public static String encrypt(String password) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            byte[] iv = generateIV();
            GCMParameterSpec parameterSpec = new GCMParameterSpec(TAG_LENGTH_BIT, iv);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
            byte[] encryptedBytes = cipher.doFinal(password.getBytes());
            // 结合 IV 和加密部分
            byte[] cipherTextWithIv = new byte[IV_SIZE + encryptedBytes.length];
            System.arraycopy(iv, 0, cipherTextWithIv, 0, IV_SIZE);
            System.arraycopy(encryptedBytes, 0, cipherTextWithIv, IV_SIZE, encryptedBytes.length);
            return Base64.getEncoder().encodeToString(cipherTextWithIv);
        } catch (Exception e) {
            throw new RuntimeException("加密异常", e);
        }
    }


    /**
     * 生成 IV
     * @return IV
     */
    public static byte[] generateIV() {
        byte[] iv = new byte[IV_SIZE];
        SecureRandom random = new SecureRandom();
        random.nextBytes(iv);
        return iv;
    }

    /**
     * 解密
     * @param encryptedText 密文
     * @return 明文
     * @throws Exception 异常
     */
    public static String decrypt(String encryptedText) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        byte[] cipherTextWithIv = Base64.getDecoder().decode(encryptedText);

        // 提取 IV
        byte[] iv = new byte[IV_SIZE];
        System.arraycopy(cipherTextWithIv, 0, iv, 0, IV_SIZE);
        // 提取密文
        byte[] cipherText = new byte[cipherTextWithIv.length - IV_SIZE];
        System.arraycopy(cipherTextWithIv, IV_SIZE, cipherText, 0, cipherText.length);

        GCMParameterSpec parameterSpec = new GCMParameterSpec(TAG_LENGTH_BIT, iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
        byte[] decryptedBytes = cipher.doFinal(cipherText);
        return new String(decryptedBytes);
    }


}
